/**
 * 
 */
package ac.svn.accesscontrol.controller;

import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import ac.notification.event.INotificationListener;
import ac.notification.event.NotificationEvent;
import ac.notification.manager.NotificationRegistrar;
import ac.svn.accesscontrol.context.Context;
import ac.svn.accesscontrol.context.IContextConstants;
import ac.svn.accesscontrol.manager.SVNInfoManager;
import ac.svn.accesscontrol.store.Dictionary;
import ac.svn.accesscontrol.store.ISVNIdentifier;
import ac.svn.accesscontrol.store.ISVNItem;
import ac.svn.accesscontrol.store.SVNUser;
import ac.svn.accesscontrol.store.UserDetails;
import ac.svn.accesscontrol.ui.panel.UserBasicInfoEditorPanel;

/**
 * @author Enigmanic
 *
 */
public class UserContactInformationController implements INotificationListener,ISVNIdentifier
,IContextConstants
{
	private UserBasicInfoEditorPanel basicInfoEditorPanel;
	private Context context;

	public UserContactInformationController( UserBasicInfoEditorPanel basicInfoEditorPanel)
	{
		//Receive target Components.
		this.basicInfoEditorPanel =basicInfoEditorPanel;

		//Register Listener on UI Components.
		registerListeners();
		
		//Register itself to receive system notifications.
		NotificationRegistrar.registerListener(this);
	}

	public class UserFinder extends KeyAdapter 
	{
		JTextField field = null;

		public UserFinder(JComponent component) 
		{
			field = (JTextField) component;
		}

		@Override
		public void keyReleased(KeyEvent e) 
		{
			if (null == field) 
			{
				field = (JTextField) e.getSource();
			}
			SwingUtilities.invokeLater(new Runnable() 
			{
				@Override
				public void run() 
				{
					SVNInfoManager infoManager = SVNInfoManager.getInfoManager();
					Set<ISVNItem> isvnItems = infoManager.getAllUsers();
					boolean matchFound = false;
					for (ISVNItem isvnItem : isvnItems) 
					{
						if (isvnItem.getId().toUpperCase().equals(field.getText().toUpperCase())) 
						{
							matchFound = true;
							break;
						}
					}

					if (true == matchFound) 
					{
						field.setBorder(new LineBorder(Color.RED));
						field.setToolTipText("A user exist with same user name, suggest another id!");
					}
					else 
					{
						field.setBorder(new LineBorder(Color.GREEN));
						field.setToolTipText("This username is available!");
					}
				}
			});
		}
	}



	private void registerListeners() 
	{
		basicInfoEditorPanel.getTextSVNUserId().addKeyListener(new UserFinder(basicInfoEditorPanel.getTextSVNUserId()));

	}

	private void loadUserAccountDetails() 
	{
		//Check if context has been shared with this controller.
		if(null != context)
		{
			SVNUser argSvnUser = (SVNUser) context.get(KEY_OPERATION_PARAM);

			//Check user information is provided in context.
			if(null != argSvnUser)
			{
				//Get contact details of user form dictionary
				UserDetails details = Dictionary.getDictionary().getUserDetails(argSvnUser.getId());
				
				//Set User Id
				basicInfoEditorPanel.getTextSVNUserId().setText(argSvnUser.getId());
				
				//If details are presents, show them.
				if (null == details) 
				{
					basicInfoEditorPanel.getTextName().setText(argSvnUser.getName());
				} 
				else 
				{
					basicInfoEditorPanel.getTextName().setText(details.getUserName());
					basicInfoEditorPanel.getTextEMailAdd().setText(details.geteMailAddress());
					basicInfoEditorPanel.getTextTeam().setText(details.getTeam());
					basicInfoEditorPanel.getTextPane().setText(details.getAboutUser());
				}
			}
		}
	}

	@Override
	public void receiveEvent(NotificationEvent notificationEvent) 
	{
		if(notificationEvent.getPropertyIdentifier().contains(CMD_SHOW_USER_CONTACT_INFO))
		{
			loadUserAccountDetails();
		}
	}

	@Override
	public Set<String> declaredSubscriptionSet() 
	{
		Set<String> subscriptionSet = new HashSet<String>();
		subscriptionSet.add(CMD_SHOW_USER_CONTACT_INFO);
		return subscriptionSet;
	}

	/**
	 * @return the context
	 */
	public Context getContext() {
		return context;
	}

	/**
	 * @param context the context to set
	 */
	public void setContext(Context context) {
		this.context = context;
	}

}
